﻿/*
 * ============ [ 废土 ] ============
 *
 * [ 维护者 ]  argoran@gmail.com
 * [  状态  ]  原型
 *
 * ============ [ 注释 ] ============
 *
 * 处理start指令的类
 *
 * ==================================
 */

using System;

using LastLandClient.Data;

using Up2Link.Client;
using Up2Link.SegString;

namespace LastLandClient.Core.CmdInterpreter
{
    public class CCmdStartClient : ICommandInterpreter
    {
        public void ExecuteCommand(string _cmd, string _args)
        {
            if (_args != null)
            {
                String[] args = _args.Split(CConst.DELIMITER_ARGUMENT);
                if (args.Length >= 2)
                {
                    StartClient(args[0].Trim(), Int32.Parse(args[1].Trim()));
                }
                else
                {
                    StartClient(args[0].Trim(), 8802);
                }
            }
            else
            {
                //CData.console.SendOutput("");
                //CData.console.SendOutput("你输入的指令是：" + _cmd);
                //CData.console.SendOutput("指令参数过少");
                CData.console.SendOutput("无效的指令");
            }
        }

        private void StartClient(String _address, Int32 _port)
        {
            if (CData.client == null)
            {
                CData.client = new CClient<CMessageSS>();
                CData.client.Start(_address, _port);
                CData.clientConnected = true;

                //CData.console.SendOutput("");
                CData.console.SendOutput("客户端启动，连接到 " + _address + ":" + _port);

                CMessageSS infoMsg = new CMessageSS(CConst.CLIENT_MESSAGE_INFO_SERVER, 0, CMessageSS.ENCODING_UTF32, "");
                CData.client.Send(infoMsg);
            }
            else
            {
                //CData.console.SendOutput("");
                CData.console.SendOutput("客户端已经启动");
            }
        }
    }
}
